// shopPages/integrals/integrals.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 2,
    more_status: true,
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    imgUrls: [],
    grids: [],
    home: [],
    jfShop: [],
    task_guize: false,
    zhinen: [],
    zhinen1: [],
    dots_f: false,
    currentSwiper: 0,
    currentSwiper1: 0,
    act_img:[]
  },
  // 轮播指示点
  swiperChange: function(e) {
    this.setData({
      currentSwiper1: e.detail.current,
    })
  },
  swiperchange: function(e) {
    this.setData({
      currentSwiper: e.detail.current,
    })
  },

  bindInte: function() {
    wx.navigateTo({
      url: '../integral/integral',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    
    // var that = this;
    // // var yinY = that.data.jfShop[1].yinY
    // var newlist = [];
    // var data = that.data.jfShop;
    // for(var i=0;i<data.length;i++){
    //   var test = data[i];
    //   var num = test.jian;
    //   // console.log(num);
    //   if (num == 0) {
    //     test.yinY = true;
    //     test.jianS = true;
    //   }
    //   // console.log(test);
    //   newlist.push(test);
    // }
    // console.log(newlist);
    // that.setData({
    //   jfShop:newlist
    // })

    // console.log(options);

    // console.log(app.globalData.userInfo);
    // wx.getSystemInfo({
    //   success: function (res) {
    //     that.setData({
    //       sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
    //       sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
    //     });
    //   }
    // });
    
    var url2 = "/mall/mallUserIntegral"
    var params2 = {
      uid: app.globalData.userInfo.id,
    };
    that.location_data(app.globalData.userInfo.id);
    wx.showLoading({
      title: '玩命加载中',
    })
    app.HttpPostSend(url2, params2, function(res) {
      wx.hideLoading();
      if (res.code > 0) {
        console.log(res)
        that.setData({
          integral: res.data.user_integral,
          // add: res.data.add_log,
          // shift: res.data.shift_log
        })
      }
    });

    
  },
  getIntegralData:function(){
    var that = this;
    var url2 = "/mall/mallIndexIntegral"
    var params2 = {
      uid: app.globalData.userInfo.id,
      city_code: app.globalData.currentCode
    };

    wx.showLoading({
      title: '玩命加载中',
    })
    app.HttpPostSend(url2, params2, function (res) {
      wx.hideLoading();
      if (res.msg !== "success") {
        app.showTipMsg(res.msg);
      } else {
        console.log(res.act_img.img)

        that.setData({
          imgUrls: res.banner_list,
          grids: res.cat_list,
          newhome: res.new_list.lists,
          rechome: res.rec_list.lists,
          jfShop: res.integral_list.lists,
          act_img: res.act_img.img
          // newSignIntegral: res.sign_log.user_integral,
          // isNewSignedArr: res.sign_log.week,
          // sign_integral: res.sign_integral,
          // newSignBtnState: res.sign_log.today_status,
          // newSignNum: res.sign_log.sign_num
          // s_list: res.recommend_list.lists.data,
          // s_list1: res.season_list.lists.data
        })
      }
    });
  },
  getMore: function() {
    var that = this;
    var new_list = that.data.jfShop;
    var url2 = "/mall/mallSecondIntegralG"
    var params2 = {
      city_code: app.globalData.currentCode,
      pageno: that.data.page,
    };
    var more_status = that.data.more_status;
    app.HttpPostSend(url2, params2, function(res) {
      wx.hideLoading();
      if (res.msg !== "success") {
        app.showTipMsg(res.msg);
      } else {
        var list = res.goods.lists;
        if (list.length < 2) {
          more_status = false;
        } else {
          more_status = true;
          var page = that.data.page;
          page += 1;
          that.setData({
            page: page
          });
        }

        for (let i = 0; i < list.length; i++) {
          new_list.push(list[i]);
        }
        // console.log(new_list);
        that.setData({
          jfShop: new_list,
          more_status: more_status
        });

      }
    });
  },
  tabClick: function(e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  toGoodsList: function(e) {
    var param = e.currentTarget.dataset.param;
    wx.navigateTo({
      url: '../../shopPages/shoplist/shoplist' + param,
    })
  },
  toMore: function(e) {
    var param = e.currentTarget.dataset.param;
    wx.navigateTo({
      url: '../../shopPages/shoplist/shoplist' + param,
    })
  },
  toGoodsDetail: function(e) {
    wx.navigateTo({
      url: "../goodsDetail/goodsDetail?id=4&title=韩国气质西装",
      // url:'../../shopPages/shoplist/shoplist'
    })
  },
  menuAction: function(e) {

  },
  toGoodsDetail_: function(e) {
    wx.navigateTo({
      url: "/pages/goodsDetail/goodsDetail?id=" + e.currentTarget.dataset.id
    })
  },
  location_data: function (uid) {
    var that = this;
    if (!app.globalData.currentCode) {
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) { //非初始化进入该页面,且未授权
            wx.showModal({
              title: '是否授权当前位置',
              content: '需要获取您的地理位置，请确认授权，否则无法获取您所需数据',
              showCancel: false,
              success: function (res) {
                if (res.cancel) {

                  wx.showToast({
                    title: '授权失败',
                    icon: 'success',
                    duration: 1000
                  })
                } else if (res.confirm) {
                  wx.openSetting({
                    success: function (dataAu) {
                      if (dataAu.authSetting["scope.userLocation"] == true) {
                        wx.showToast({
                          title: '授权成功',
                          icon: 'success',
                          duration: 1000
                        })
                        //再次授权，调用getLocationt的API
                        that.getLocation(uid);
                      } else {
                        wx.showToast({
                          title: '授权失败',
                          icon: 'success',
                          duration: 1000
                        })
                      }
                    }
                  })
                }
              }
            })
          } else if (res.authSetting['scope.userLocation'] == undefined) { //初始化进入

            that.getLocation(uid);
          } else { //授权后默认加载

            that.getLocation(uid);
          }
        }
      })
    }else{
      that.getIntegralData()
    }
  },
  getLocation: function (uid) {

    const that = this;
    wx.showLoading({
      title: '定位中',
    })
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {

        // console.log("测试：" + res);
        let latitude = res.latitude
        let longitude = res.longitude
        app.globalData.latitude = res.latitude;
        app.globalData.longitude = res.longitude;
        let keys = "PCZBZ-6Y3W6-HVVSM-E22YX-JTSYO-QJBZD"
        app.HttpPostSend('/mall/get_location', {
          latitude: latitude,
          longitude: longitude,
          uid: uid
        }, function (res) {
          wx.hideLoading();
          var re_data = res;
          if (re_data.code > 0) {
            that.setData({
              currentCityCode: re_data.adcode,
              currentCity: re_data.district,
              area_human: re_data.area_human,
              indicatorDots: false,
              currentSwiper: 0,
              currentSwiper1: 0,
              autoplay: true,
              autoplay1: false,
              interval: 3000,
              duration: 500,
              circular: true,
            })
            app.globalData.currentCity = re_data.district;
            app.globalData.currentCode = re_data.adcode;
            that.getIntegralData()
          } else {
            wx.showModal({
              title: '提示',
              content: '获取定位失败，请手动选择地区',
              showCancel: false,
              success: function (res) {
                if (res.cancel) {

                  wx.showToast({
                    title: '失败',
                    icon: 'success',
                    duration: 1000
                  })
                } else if (res.confirm) {
                  wx.navigateTo({
                    url: '/shopPages/cityList/cityList',
                  })
                  return false;
                }
              }
            })
          }

        });

      },
      fail: res => {
        wx.hideLoading();
        console.log('授权失败');
        wx.showModal({
          title: '是否授权当前位置',
          content: '需要获取您的地理位置，请确认授权，否则无法获取您所需数据',
          showCancel: false,
          success: function (res) {
            wx.hideLoading();
            if (res.cancel) {

              wx.showToast({
                title: '授权失败',
                icon: 'success',
                duration: 1000
              })
            } else if (res.confirm) {
              wx.openSetting({
                success: function (dataAu) {
                  if (dataAu.authSetting["scope.userLocation"] == true) {
                    wx.showToast({
                      title: '授权成功',
                      icon: 'success',
                      duration: 1000
                    })
                    //再次授权，调用getLocationt的API
                    that.getLocation();
                  } else {
                    wx.showToast({
                      title: '授权失败',
                      icon: 'success',
                      duration: 1000
                    })
                  }
                }
              })
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    // console.log(that.data.more_status);
    if (that.data.more_status == true) {
      var that = this;
      // 显示加载图标
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1
      that.getMore();

    } else {
      wx.hideLoading();
    }
  },
})